<?php
class public_model extends CI_Model{
	function public_model(){  
         parent::__construct();  
         $this->load->helper('url');
         $this->load->model('login/membership_model');
  	}  
	function getTemplateForUrl(){
		$webstoreObj['templatecode'] = TEMPLATE_CODE;
		$webstoreObj['webstore']="";
		$webstoreObj['id']="0";
		return $webstoreObj;
	}
	
	function getNonSSLURL(){
		$force_ssl = $this->getConfig('force_all_link_to_ssl', 'customconfig');
		return ($force_ssl == "yes" ? "https://" : "http://") .$_SERVER['HTTP_HOST']. "/";
	}
	
	function getSystemParameterValue($paramname, $seller_id = 0){
		$this->db->where('name',$paramname);
		$query = $this->db->get('systemparameters');	
		if(!empty($query) && $query->num_rows() > 0) {
			return $query->first_row()->value;	
		}
		else{//for mobile
			$this->db->where('name',$paramname);
			$query = $this->db->get('systemparameters');
			if(!empty($query) && $query->num_rows() > 0) {
				return $query->first_row()->value;
			}
		}
		return false;
	}
	
	function getLanguage(){
		$language = $this->session->userdata('language');
		if(empty($language)){
			$language = $this->config->item('language');
			$this->session->set_userdata(array('language' => $language));
		}
		return $language;
	}
	
	function getConfig($name, $file){
		$objCI = &get_instance();
        $objCI->load->config($file);
        return $objCI->config->item($name);
	}
	
	function getBaseSSLUrl(){
		$use_ssl = $this->getConfig('use_ssl', 'customconfig');
		$url = base_url();
		if($use_ssl == "yes") $url = $this->getConfig('ssl_url', 'customconfig');
		return $url;
	}
	
	function getSelectboxArray($array, $id_variable, $value_variable){
		$rs = array("" => "");
		if(count($array) > 0){
			foreach($array as $item){
				$rs[$item->$id_variable] = $item->$value_variable; 
			}
		}
		return $rs;
	}
    
    function getCitys()
    {
        $this->db->from('k_city');
		$this->db->select("*");
		$query = $this->db->get();
		$rs = array();	
		if($query->num_rows() > 0) {
			$rs = $query->result();	
		}
		return $rs;
    }
    
    function getFooter()
    {        
        $this->db->from('k_footer');
		$this->db->select("*");
		$query = $this->db->get();
		$rs = array();	
		if($query->num_rows() > 0) {
			$rs = $query->result();	
		}
		return $rs;
    }
    
    function getContentFooterByID($id)
    {   
        $this->db->from('k_footer_content');
        $this->db->where('id_footer', $id);
        $query = $this->db->get();
        $rs = array();	
		if($query->num_rows() > 0) {
			$rs = $query->result();	
		}
		return $rs;
    }
    
    function getNavbar($options)
    {   
        $this->db->from('k_navbar');
        if (isset($options['enable'])) 
            $this->db->where('enable', $options['enable']);
        $query = $this->db->get();
        $rs = array();	
		if($query->num_rows() > 0) {
			$rs = $query->result();	
		}
		return $rs;
    }
    function getNavbarContent($options = null, $navbar_id = null)
    {
        $this->db->from('k_navbar_content');
        if (isset($navbar_id) && $navbar_id != null ) 
            $this->db->where('id_navbar', $navbar_id);
        $query = $this->db->get();
        $rs = array();	
		if($query->num_rows() > 0) {
			$rs = $query->result();	
		}
		return $rs;
    }
    
    function getCategories($options)
    {   
        $this->db->from('k_categories');
        if (isset($options['enable'])) 
            $this->db->where('enable', $options['enable']);
        if (isset($options['limit']) && $options['limit'] != '')
        {
            if (isset($options['offset']))
                $this->db->limit($options['limit'], $options['offset']);    
        }
        $this->db->order_by('display_order', 'asc');
        $this->db->order_by('name', 'asc');
        $query = $this->db->get();
        $rs = array();	
		if($query->num_rows() > 0) {
			$rs = $query->result();
            	
            /* get number deal*/
            $city_id = $this->session->userdata('city_session');	
            for($i = 0; $i < count($rs); $i++)
            {
                $number = $this->getCountDeal(array('category_id' => $rs[$i]->id, 'city_id'=>$city_id));
                $rs[$i]->number_of_deal = $number;
            }
		}
		return $rs;
    }
    function getCategory($options)
    {   
        $this->db->from('k_categories');
        if (isset($options['enable'])) 
            $this->db->where('enable', $options['enable']);
        if (isset($options['category_id']))
            $this->db->where('id', $options['category_id']);
        $query = $this->db->get();
        $rs = $query->row();
        
        /* get number deal*/
        $city_id = $this->session->userdata('city_session');	
       	$number = $this->getCountDeal(array('category_id' => $rs->id, 'city_id'=>$city_id));
        $rs->number_of_deal = $number;
        return $rs;
    }
    function getCountDeal($options)
    {
        $this->db->from('k_deal');
        if (isset($options['city_id']) && $options['city_id']!='')
            $this->db->where('id_city', $options['city_id']);
        if (isset($options['category_id']) && $options['category_id']!='')
            $this->db->where('id_categories', $options['category_id']);
        $query = $this->db->get();
		return $query->num_rows(); 
    }
     function getDeal($options)
    {
        $this->db->from('k_deal');
        if (isset($options['city_id']) && $options['city_id']!='')
            $this->db->where('id_city', $options['city_id']);
        if (isset($options['category_id']) && $options['category_id']!='')
            $this->db->where('id_categories', $options['category_id']);
        if (isset($options['id']) && $options['id'] != '')
            $this->db->where('id', $options['id']);
        if (isset($options['limit']) && $options['limit'] != '')
        {
            if (isset($options['offset']))
                $this->db->limit($options['limit'], $options['offset']);    
        }
        if (isset($options['desc']))
            $this->db->order_by($options['desc'],'desc');
            
        $this->db->select("k_deal.*, TIMESTAMPDIFF(SECOND, NOW(), k_deal.end_day) AS time_to_expire", false);
        $query = $this->db->get();
        $rs = array();
		if($query->num_rows() > 0) {
			$rs = $query->result();
            //get category	
            for($i = 0; $i < count($rs); $i++)
            {
                $category = $this->getCategory(array('category_id' => $rs[$i]->id_categories));
                if ($category != null)
                {
                    $rs[$i]->category_name = $category->name;
                    $rs[$i]->view = $category->view;
                }
                else
                {
                    $rs[$i]->category_name = 'no_category_name';
                    $rs[$i]->view = VIEW_PRICE;
                }
            }
		}
		return $rs;
    }
    
    function getDealById($deal_id)
    {
        $this->db->from('k_deal');
        $this->db->where('id', $deal_id);
        $this->db->select("k_deal.*, TIMESTAMPDIFF(SECOND, NOW(), k_deal.end_day) AS time_to_expire", false);
        $query = $this->db->get();
        $rs = array();
        $rs = $query->row();
        if ($rs == '' || count($rs) == 0)
            return null;
        else
        {
            $is_sold_out = ($rs->number_voucher_max <= $rs->count_voucher_buy) || ( $rs->time_to_expire <= 0);   
            if ($is_sold_out)
                return null; 
            //get category	
            $category = $this->getCategory(array('category_id' => $rs->id_categories));
            if ($category != null)
            {
                $rs->category_name = $category->name;
                $rs->view = $category->view;
            }
            else
            {
                $rs->category_name = 'no_category_name';
                $rs->view = VIEW_PRICE;
            }
        }
		return $rs;
    }
}
